//
//  AddNewAddressViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/7/22.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "AddNewAddressViewController.h"

#import "AddAddressView.h"//添加新地址View


@interface AddNewAddressViewController ()

@end

@implementation AddNewAddressViewController
{
    AddAddressView *_addAddressView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setNav];
    
    [self setView];
}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    self.title = @"添加新地址";
    
    UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAddress)];
    self.navigationItem.rightBarButtonItem = save;
}

- (void)setView{
    _addAddressView = [[[NSBundle mainBundle] loadNibNamed:@"AddAddressView" owner:nil options:nil] lastObject];
    _addAddressView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
    __weak typeof (_addAddressView) weakView = _addAddressView;
    _addAddressView.addNewAddressClick = ^(int temp){
        switch (temp) {
            case 0:
                NSLog(@"收货人");
                weakView.name.text = @"啦啦啦";
                break;
            case 1:
                NSLog(@"联系电话");
                weakView.phone.text = @"15757855404";
                weakView.addPhoneImage.hidden = YES;
                break;
            case 2:
                NSLog(@"所在地区");
                weakView.address.text = @"浙江 杭州";
                weakView.address.textColor = [UIColor blackColor];
                break;
            case 3:
                NSLog(@"街道");
                weakView.street.text = @"西湖区";
                weakView.street.textColor = [UIColor blackColor];
                weakView.detailAddress.text = @"西湖科技大厦 D座 南楼 4楼 小橙网络";
                weakView.detailAddress.textColor = [UIColor blackColor];
                break;
            default:
                break;
        }
    };
    [self.view addSubview:_addAddressView];
}


#pragma mark === 保存新地址 ===
- (void)saveAddress{
    if (![_addAddressView.name.text isEqualToString:@""]) {
        if (![_addAddressView.phone.text isEqualToString:@""]) {
            if (![_addAddressView.address.text isEqualToString:@"请选择"]) {
                if (![_addAddressView.street.text isEqualToString:@"请选择"]) {
                    if (![_addAddressView.detailAddress.text isEqualToString:@"请填写详细地址, 不少于5个字"]) {
                        [self showAlert:@"确认保存新地址?"];
                    }else [self showAlert:@"请填写详细地址"];
                }else [self showAlert:@"请填写街道信息"];
            }else [self showAlert:@"请选择所在地区"];
        }else [self showAlert:@"请填写联系电话"];
    }else [self showAlert:@"请填写收货人姓名"];
    
}

//弹出警示框
- (void)showAlert:(NSString *)string{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@", string] message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    //信息完善,确认保存?
    if ([string isEqualToString:@"确认保存新地址?"]) {
        UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *done) {
            NSLog(@"network-------");
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alert addAction:done];
        
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancle];
        
        
    }else{
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancle];
    }
    
    
    [self presentViewController:alert animated:YES completion:nil];
}


@end
